package com.info.zhiduoduo.common.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class WaterMarkUtil {

	public static void createWaterMarkByText(InputStream srcImgStream, String logoText, File outputImageFile, double degree) {
		OutputStream os = null;
		try {
			Image srcImg = ImageIO.read(srcImgStream);

			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

			Graphics2D graphics = buffImg.createGraphics();
			graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

			if (degree > 0) {
				graphics.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
			}
			graphics.setColor(Color.RED);
			graphics.setFont(new Font("宋体", Font.BOLD, 36));

			float alpha = 0.8f;
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

			graphics.drawString(logoText, buffImg.getWidth() / 3, buffImg.getHeight() / 2);
			graphics.dispose();

			os = new FileOutputStream(outputImageFile);
			// 生成图片
			ImageIO.write(buffImg, "JPG", os);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != os)
					os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void createWaterMarkByIcon(InputStream srcImageStream, File logoImageFile, File outputImageFile, double degree) {

		OutputStream os = null;
		try {
			Image srcImg = ImageIO.read(srcImageStream);

			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

			Graphics2D graphics = buffImg.createGraphics();
			graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

			//水印logo
			ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
			Image logoImg = logoImgIcon.getImage();
			int newWidth = (int) (buffImg.getWidth() * 0.30f);
			Image scaleLogo = logoImg.getScaledInstance(newWidth, -1, Image.SCALE_SMOOTH);

			//旋转
			if (degree > 0) {
				graphics.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getWidth() / 2);
			}

			float alpha = 0.3f; // 透明度
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

			//水印 的位置
			int x = buffImg.getWidth() / 2 - scaleLogo.getWidth(null) / 2;
			int y = buffImg.getHeight() / 2 - scaleLogo.getHeight(null) / 2;
			graphics.drawImage(scaleLogo, x, y, null);
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
			graphics.dispose();

			os = new FileOutputStream(outputImageFile);
			// 生成图片
			ImageIO.write(buffImg, "JPG", os);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != os)
					os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
